import React, { Component, Fragment } from "react";
// antd
import { Table, Button, Col, Pagination, Form, Input, Select, Tag } from "antd";

// http
import http from '../../utils/request';

const { Option } = Select;


class EmgVul extends Component {
    constructor(props){
        super(props)
        this.state = {
            loading: false,

            // 分页
            total: '',
            page: 1,
            page_number: 10,

            // 搜索
            search_instance_platform: null,
            search_instance_account_name: null,
            search_emgvul_name: null,
            search_option_account_name: [],
            search_option_platform: [],
            search_option_account: [],

            selectedRowKeys: [],
            
            // 表格
            dataSource: [],
            columns: [
                {title: "云平台", dataIndex: "cloud-platform", key: "cloud-platform", width: "250px",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.platform}-{rowData.account_name}</Tag>
                            </div>
                        )
                    }
                },
                {title: "名称", dataIndex: "emgvul_name", key: "emgvul_name",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="blue">{rowData.emgvul_name}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "类型", dataIndex: "emgvul_type", key: "emgvul_type",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.emgvul_type}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "数量", dataIndex: "emgvul_count", key: "emgvul_count"},
                {title: "时间", dataIndex: "last_time", key: "last_time"}
            ],
        }
        this.formRefLabel = React.createRef();
    }

    componentDidMount() {
        this.loadData()
        this.loadDataPlatform()
    }

    loadDataPlatform = () => {
        const requestData = {
            'page': 1,
            'limit': 1000,
        }
        http.get('/cloud/platform', {params: requestData}).then(response=>{
            const search_option_platform = []
            const resp_data = response.data.data.items
            for (let i = 0; i < resp_data.length; i++) {
                search_option_platform.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
            }
            this.setState({
                search_option_platform: search_option_platform
            })
        })
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'emgvul_name__contains': this.state.search_emgvul_name,
            'account_name': this.state.search_instance_account_name,
            'platform': this.state.search_instance_platform,
        }

        // 获取database列表
        http.get('/security/emgvul', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    // 切换每页数量
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

     /** 搜索 */
    onFinishSearch = (data) => {
        this.setState({
            search_emgvul_name: data.emgvul_name ? data.emgvul_name : null,
            search_instance_platform: data.platform ? data.platform : null,
            search_instance_account_name: data.account_name ? data.account_name : null,
            page: 1
        })
        this.loadData()
    }

    // 搜索切换云平台
    onChangePlatform = (value) => {
        if (value) {
            this.setState({
                search_instance_platform: value
            })

            const requestData = {
                'page': 1,
                'limit': 1000,
                'platform': value
            }
    
            http.get('/cloud/account', {params: requestData}).then(response=>{
                const search_option_account = []
                const resp_data = response.data.data.items
                for (let i = 0; i < resp_data.length; i++) {
                    search_option_account.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
                }
                this.setState({
                    search_option_account: search_option_account
                })
            })
    

        } else {
            this.setState({
                search_option_account: '',
            })
        }
    }


    // 搜索云账户
    onChangeAccountName = (value) => {
        this.setState({
            search_instance_account_name: value ? value : null
        })
    }

    // 批量选中标签
    onSelectChange = (selectedRowKeys) => {
        this.setState({
            selectedRowKeys: selectedRowKeys
        })
    }

    render(){
        const { loading, columns, dataSource, page, total, search_option_platform, search_option_account, selectedRowKeys} = this.state
        const rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        }

        // <Button type="primary" onClick={() => this.onLabelBtn()} >批量打标签</Button>
        // <Button type="primary" onClick={this.onAddDatabaseBtn}>添加资产</Button>


        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        var myStyleForm = {marginRight: '10px', marginTop: '5px'}

        return (
            <Fragment>
                <Form layout="inline" onFinish={this.onFinishSearch} size="small">
                    <Form.Item label="漏洞名称" name="emgvul_name" style={myStyleForm}>
                        <Input placeholder="漏洞名称"  />
                    </Form.Item>

                    <Form.Item label="云平台" name="platform" style={myStyleForm}>
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 200 }}
                            placeholder="请选择平台"
                            optionFilterProp="children"
                            onChange={this.onChangePlatform}
                            filterOption={(input, option) =>
                            option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                            }
                        >
                            {search_option_platform}
                        </Select>
                    </Form.Item>   

                    <Form.Item label="云账户" name="account_name" style={myStyleForm}>
                            <Select
                                allowClear
                                showSearch
                                style={{ width: 200 }}
                                placeholder="请选择云账户"
                                onChange={this.onChangeAccountName}
                                >
                                {search_option_account}
                            </Select>                      
                    </Form.Item>

    
                    <Form.Item shouldUpdate={true} style={myStyleForm}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>

                <div style={myStyle1}></div>

                <Table
                    // size="small"
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    // scroll={{ y: 600, x: 2000 }}
                >
                </Table>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            // pageSizeOptions={pagination_size_option}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

            </Fragment>
        )
    }
}

export default EmgVul;